﻿using DevExpress.Utils;
using DevExpress.XtraBars;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraEditors.Mask;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraTab;
using EFERP;
using HRM.DICTIONARY.DAL;
using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows.Forms;

namespace HRM.DICTIONARY.UI
{
    public partial class xfrmAllOption : XtraForm
    {
        private ICommon<sys_option> _exeOption;
        private ICommon<dic_pit> _exePit;
        private ICommon<dic_timekeeper_formula> _exeTimeKeeperFromular;
        private ICommon<dic_signer> _exeSigner;
        private ICommon<dic_minimumsalary> _exeMiniSalary;
        private ICommon<dic_salary_formula> _exeSalaryFromular;

        private List<sys_option> _listOptions;
        private List<dic_timekeeper_formula> _listTimekeeperFormulas;
        private List<dic_minimumsalary> _listMinimumsalaries;
        //private List<dic_pit> _listPits;
        private List<dic_salary_formula> _listSalaryFormulas;
        //private List<dic_signer> _listSigners;
        private TabControl _currTabControl;
        private enum TabControl
        {
            TuyChonChung,
            CauHinhGuiMail,
            HopDongLaoDong,
            ChamCong,
            MucLuongToiThieu,
            DinhMucTinhLuong,
            InBaoCao,
            CanhBao
        }

        public xfrmAllOption()
        {
            InitializeComponent();
        }

        private void xfrmAllOption_Load(object sender, EventArgs e)
        {
            _exeOption = new Common<sys_option>(StaticVariable.ERPEn);
            _exeMiniSalary = new Common<dic_minimumsalary>(StaticVariable.ERPEn);
            _exePit = new Common<dic_pit>(StaticVariable.ERPEn);
            _exeSalaryFromular = new Common<dic_salary_formula>(StaticVariable.ERPEn);
            _exeSigner = new Common<dic_signer>(StaticVariable.ERPEn);
            _exeTimeKeeperFromular = new Common<dic_timekeeper_formula>(StaticVariable.ERPEn);

            _listOptions = _exeOption.GetList();
            _listTimekeeperFormulas = _exeTimeKeeperFromular.GetList();
            _listMinimumsalaries = _exeMiniSalary.GetList();
            //_listPits = _exePit.GetList();
            //_listSigners = _exeSigner.GetList();
            _listSalaryFormulas = _exeSalaryFromular.GetList();

            this.barManager1.SetPopupContextMenu(this.gcList, this.ppMenu);
            _exePit.GC = gcList;
            _exePit.GV = gbList;
            _exePit.ColIDName = "pitid";

            _exeSigner.GC = gcBaoCao;
            _exeSigner.GV = gbBaoCao;
            _exeSigner.ColIDName = "signercode";
            _currTabControl = TabControl.TuyChonChung;
            LoadAllOptionToUi(_currTabControl);
        }

        private void LoadAllOptionToUi(TabControl tab)
        {
            try
            {
                List<sys_option> listOption = _listOptions;
                List<sys_option> listSelect;
                sys_option objOption;
                _currTabControl = tab;
                switch (tab)
                {
                    case TabControl.TuyChonChung:

                        #region CauHinhChung

                        listSelect = listOption.Where(s => s.description == "alloption").ToList();

                        objOption = listSelect.SingleOrDefault(s => s.option_id == "MonthDefault");
                        if (objOption != null) dtMonthDefault.DateTime = DateTime.Parse(objOption.optionvalue);

                        objOption = listSelect.SingleOrDefault(s => s.option_id == "ShowDiagram");
                        if (objOption != null) cheShowDiagram.Checked = bool.Parse(objOption.optionvalue);

                        objOption = listSelect.SingleOrDefault(s => s.option_id == "ShowWorkdesk");
                        if (objOption != null) cheShowWorkdesk.Checked = bool.Parse(objOption.optionvalue);

                        objOption = listSelect.SingleOrDefault(s => s.option_id == "ShowWelcome");
                        if (objOption != null) cheShowWelcome.Checked = bool.Parse(objOption.optionvalue);

                        objOption = listSelect.SingleOrDefault(s => s.option_id == "ShowTimekeeper");
                        if (objOption != null) cheShowTimekeeper.Checked = bool.Parse(objOption.optionvalue);

                        objOption = listSelect.SingleOrDefault(s => s.option_id == "ShowSalary");
                        if (objOption != null) cheShowSalary.Checked = bool.Parse(objOption.optionvalue);

                        objOption = listSelect.SingleOrDefault(s => s.option_id == "Update");
                        if (objOption != null) rdUpdate.SelectedIndex = int.Parse(objOption.optionvalue);

                        #endregion CauHinhChung

                        break;

                    case TabControl.CauHinhGuiMail:

                        #region CauHinhMail

                        listSelect =
                            listOption.Where(s => s.description == "emailoption").ToList();

                        objOption = listSelect.SingleOrDefault(s => s.option_id == "ShowSendMail");
                        if (objOption != null) cheShowSendMail.Checked = bool.Parse(objOption.optionvalue);

                        objOption = listSelect.SingleOrDefault(s => s.option_id == "Host");
                        if (objOption != null) txtHost.Text = objOption.optionvalue;

                        objOption = listSelect.SingleOrDefault(s => s.option_id == "Port");
                        if (objOption != null) calPort.EditValue = objOption.optionvalue;

                        objOption = listSelect.SingleOrDefault(s => s.option_id == "EnableSSL");
                        if (objOption != null) cheEnableSSL.Checked = bool.Parse(objOption.optionvalue);

                        #endregion CauHinhMail

                        break;

                    case TabControl.HopDongLaoDong:

                        #region HopDongLaoDong

                        listSelect = listOption.Where(s => s.description == "contractoption").ToList();

                        objOption = listSelect.SingleOrDefault(s => s.option_id == "Signer");
                        if (objOption != null) txtSigner.Text = objOption.optionvalue;

                        objOption = listSelect.SingleOrDefault(s => s.option_id == "SignerNationality");
                        if (objOption != null) txtSignerNationality.Text = objOption.optionvalue;

                        objOption = listSelect.SingleOrDefault(s => s.option_id == "SignerPosition");
                        if (objOption != null) txtSignerPosition.Text = objOption.optionvalue;

                        objOption = listSelect.SingleOrDefault(s => s.option_id == "FilePath0");
                        if (objOption != null) txtFilePath0.Text = objOption.optionvalue;

                        objOption = listSelect.SingleOrDefault(s => s.option_id == "FilePath1");
                        if (objOption != null) txtFilePath1.Text = objOption.optionvalue;

                        objOption = listSelect.SingleOrDefault(s => s.option_id == "FilePath2");
                        if (objOption != null) txtFilePath2.Text = objOption.optionvalue;

                        objOption = listSelect.SingleOrDefault(s => s.option_id == "FilePath3");
                        if (objOption != null) txtFilePath3.Text = objOption.optionvalue;

                        objOption = listSelect.SingleOrDefault(s => s.option_id == "NumberDayWarning");
                        if (objOption != null) calNumberDayWarning.EditValue = int.Parse(objOption.optionvalue);

                        #endregion HopDongLaoDong

                        break;

                    case TabControl.ChamCong:

                        #region Cham Cong

                        //---------------------------------------------------timekeeperoption------------------------------------------------------------------------------------------------

                        listSelect = listOption.Where(s => s.description == "timekeeperoption").ToList();

                        objOption = listSelect.SingleOrDefault(s => s.option_id == "TimeKeeperTableDefault");
                        if (objOption != null)
                            cboTimeKeeperTableDefault.SelectedIndex = int.Parse(objOption.optionvalue);

                        objOption = listSelect.SingleOrDefault(s => s.option_id == "ShowAssignmentDialog");
                        if (objOption != null) cheShowAssignmentDialog.Checked = bool.Parse(objOption.optionvalue);

                        objOption = listSelect.SingleOrDefault(s => s.option_id == "ShowPetitionDialog");
                        if (objOption != null) cheShowPetitionDialog.Checked = bool.Parse(objOption.optionvalue);

                        objOption = listSelect.SingleOrDefault(s => s.option_id == "AutoAssignment");
                        if (objOption != null) cheAutoAssignment.Checked = bool.Parse(objOption.optionvalue);

                        objOption = listSelect.SingleOrDefault(s => s.option_id == "AutoPetition");
                        if (objOption != null) cheAutoPetition.Checked = bool.Parse(objOption.optionvalue);

                        objOption = listSelect.SingleOrDefault(s => s.option_id == "MondayColor");
                        if (objOption != null) colorMonday.Color = Color.FromArgb(int.Parse(objOption.optionvalue));

                        objOption = listSelect.SingleOrDefault(s => s.option_id == "TuesdayColor");
                        if (objOption != null) colorTuesday.Color = Color.FromArgb(int.Parse(objOption.optionvalue));

                        objOption = listSelect.SingleOrDefault(s => s.option_id == "WednesdayColor");
                        if (objOption != null) colorWednesday.Color = Color.FromArgb(int.Parse(objOption.optionvalue));

                        objOption = listSelect.SingleOrDefault(s => s.option_id == "ThursdayColor");
                        if (objOption != null) colorThursday.Color = Color.FromArgb(int.Parse(objOption.optionvalue));

                        objOption = listSelect.SingleOrDefault(s => s.option_id == "FridayColor");
                        if (objOption != null) colorFriday.Color = Color.FromArgb(int.Parse(objOption.optionvalue));

                        objOption = listSelect.SingleOrDefault(s => s.option_id == "SaturdayColor");
                        if (objOption != null) colorSaturday.Color = Color.FromArgb(int.Parse(objOption.optionvalue));

                        objOption = listSelect.SingleOrDefault(s => s.option_id == "SundayColor");
                        if (objOption != null) colorSunday.Color = Color.FromArgb(int.Parse(objOption.optionvalue));

                        objOption = listSelect.SingleOrDefault(s => s.option_id == "HolidayColor");
                        if (objOption != null) colorHoliday.Color = Color.FromArgb(int.Parse(objOption.optionvalue));

                        //-----------------------------TimekeeperFormula---------------------------------------------------------------------------------------------------------------

                        dic_timekeeper_formula obiTimekeeperFormula =
                            _listTimekeeperFormulas.SingleOrDefault(
                                s => s.formulaid == Guid.Parse("00000000-0000-0000-0000-000000000000"));

                        if (obiTimekeeperFormula != null)
                        {
                            //saturday
                            if (obiTimekeeperFormula.isovertimesaturday != null)
                                cheIsOvertimeSaturday.Checked = (bool)obiTimekeeperFormula.isovertimesaturday;
                            if (obiTimekeeperFormula.overtimesaturdaytype != null)
                                cboOvertimeSaturdayType1.SelectedIndex = (int)obiTimekeeperFormula.overtimesaturdaytype;
                            if (obiTimekeeperFormula.begintimesaturday != null)
                                teBeginTimeSaturday.EditValue = (DateTime)obiTimekeeperFormula.begintimesaturday;
                            if (obiTimekeeperFormula.minimumminutesaturday != null)
                                calMinimumMinuteSaturday.EditValue = (int)obiTimekeeperFormula.minimumminutesaturday;
                            if (obiTimekeeperFormula.maximumminutesaturday != null)
                                calMaximumMinuteSaturday.EditValue = (int)obiTimekeeperFormula.maximumminutesaturday;

                            if (cheIsOvertimeSaturday.Checked)
                            {
                                cboOvertimeSaturdayType1.Enabled = true;
                                pnSubOvertimeSaturday.Enabled = cboOvertimeSaturdayType1.SelectedIndex > 0;
                            }
                            else
                            {
                                cboOvertimeSaturdayType1.Enabled = false;
                                pnSubOvertimeSaturday.Enabled = false;
                            }

                            //sunday
                            if (obiTimekeeperFormula.isovertimesunday != null)
                                cheIsOvertimeSunday.Checked = (bool)obiTimekeeperFormula.isovertimesunday;
                            if (obiTimekeeperFormula.overtimesundaytype != null)
                                cboOvertimeSundayType.SelectedIndex = (int)obiTimekeeperFormula.overtimesundaytype;
                            if (obiTimekeeperFormula.begintimesunday != null)
                                teBeginTimeSunday.EditValue = (DateTime)obiTimekeeperFormula.begintimesunday;
                            if (obiTimekeeperFormula.minimumminutesunday != null)
                                calMinimumMinuteSunday.EditValue = (int)obiTimekeeperFormula.minimumminutesunday;
                            if (obiTimekeeperFormula.maximumminutesunday != null)
                                calMaximumMinuteSunday.EditValue = (int)obiTimekeeperFormula.maximumminutesunday;

                            if (cheIsOvertimeSunday.Checked)
                            {
                                cboOvertimeSundayType.Enabled = true;
                                pnSubOvertimeSunday.Enabled = cboOvertimeSundayType.SelectedIndex > 0;
                            }
                            else
                            {
                                cboOvertimeSundayType.Enabled = false;
                                pnSubOvertimeSunday.Enabled = false;
                            }

                            //Holiday
                            if (obiTimekeeperFormula.isovertimeholiday != null)
                                cheIsOvertimeHoliday.Checked = (bool)obiTimekeeperFormula.isovertimeholiday;
                            if (obiTimekeeperFormula.overtimeholidaytype != null)
                                cboOvertimeHolidayType.SelectedIndex = (int)obiTimekeeperFormula.overtimeholidaytype;
                            if (obiTimekeeperFormula.begintimeholiday != null)
                                teBeginTimeHoliday.EditValue = (DateTime)obiTimekeeperFormula.begintimeholiday;
                            if (obiTimekeeperFormula.minimumminuteholiday != null)
                                calMinimumMinuteHoliday.EditValue = (int)obiTimekeeperFormula.minimumminuteholiday;
                            if (obiTimekeeperFormula.maximumminuteholiday != null)
                                calMaximumMinuteHoliday.EditValue = (int)obiTimekeeperFormula.maximumminuteholiday;

                            if (cheIsOvertimeHoliday.Checked)
                            {
                                cboOvertimeHolidayType.Enabled = true;
                                this.pnSubOvertimeHoliday.Enabled = cboOvertimeHolidayType.SelectedIndex > 0;
                            }
                            else
                            {
                                cboOvertimeHolidayType.Enabled = false;
                                this.pnSubOvertimeHoliday.Enabled = false;
                            }

                            //
                            if (obiTimekeeperFormula.begintimenight != null)
                                teBeginTimeNight.EditValue = (DateTime)obiTimekeeperFormula.begintimenight;
                            if (obiTimekeeperFormula.endtimenight != null)
                                teEndTimeNight.EditValue = (DateTime)obiTimekeeperFormula.endtimenight;
                            if (obiTimekeeperFormula.numberdaystandard != null)
                                calNumberDayStandard.EditValue = (Double)obiTimekeeperFormula.numberdaystandard;
                            if (obiTimekeeperFormula.numberhourstandard != null)
                                calNumberHourStandard.EditValue = (Double)obiTimekeeperFormula.numberhourstandard;
                        }

                        #endregion Cham Cong

                        break;

                    case TabControl.MucLuongToiThieu:

                        #region Muc Luong Toi Thieu

                        dic_minimumsalary objMinimumsalary =
                            _listMinimumsalaries.SingleOrDefault(
                                s => s.minimumsalaryid == Guid.Parse("72B634FD-CFDB-409B-BA6F-66D34A6BECC9"));
                        if (objMinimumsalary != null)
                        {
                            if (objMinimumsalary.money != null)
                                this.calMoney.EditValue = (decimal)objMinimumsalary.money;
                            if (objMinimumsalary.date != null) this.dtDate.DateTime = (DateTime)objMinimumsalary.date;
                            if (objMinimumsalary.decidenumber != null)
                                this.txtDecideNumber.Text = objMinimumsalary.decidenumber;
                            if (objMinimumsalary.filepath != null) this.txtFilePath.Text = objMinimumsalary.filepath;
                            if (objMinimumsalary.person != null) this.txtPerson.Text = objMinimumsalary.person;
                        }

                        #endregion Muc Luong Toi Thieu

                        break;

                    case TabControl.DinhMucTinhLuong:

                        #region Dinh Muc Tinh Luong

                        dic_salary_formula objSalaryFormula =
                            _listSalaryFormulas.SingleOrDefault(
                                s => s.formulaid == Guid.Parse("00000000-0000-0000-0000-000000000000"));
                        if (objSalaryFormula != null)
                        {
                            this.calSocialInsurance.EditValue = (Double)objSalaryFormula.socialinsurance;
                            this.calHealthInsurance.EditValue = (Double)objSalaryFormula.healthinsurance;
                            this.calUnemploymentInsurance.EditValue = (Double)objSalaryFormula.unemploymentinsurance;

                            this.calSocialInsurance1.EditValue = (Double)objSalaryFormula.socialinsurance1;
                            this.calHealthInsurance1.EditValue = (Double)objSalaryFormula.healthinsurance1;
                            this.calUnemploymentInsurance1.EditValue = objSalaryFormula.unemploymentinsurance1;
                            this.calMaximumInsurance.EditValue = objSalaryFormula.maximuminsurance;
                            this.cboUnionType.SelectedIndex = (int)objSalaryFormula.uniontype;

                            this.calUnionValue.EditValue = (int)objSalaryFormula.unionvalue;
                            this.cboUnionType1.SelectedIndex = (int)objSalaryFormula.uniontype1;
                            this.calUnionValue1.EditValue = objSalaryFormula.unionvalue1;
                            this.calMaximumUnion.EditValue = objSalaryFormula.maximumunion;
                            this.calIncomeTax.EditValue = objSalaryFormula.incometax;
                            this.calPersonIncomeTax.EditValue = objSalaryFormula.personincometax;
                            this.calCoefficientOvertimeWorkday.EditValue = objSalaryFormula.coefficientovertimeworkday;
                            this.calCoefficientOvertimeSunday.EditValue = objSalaryFormula.coefficientovertimesunday;
                            this.calCoefficientOvertimeHoliday.EditValue = objSalaryFormula.coefficientovertimeholiday;
                            this.calCoefficientOvertimeNightWorkday.EditValue =
                                objSalaryFormula.coefficientovertimeworkday;
                            this.calCoefficientOvertimeNightSunday.EditValue =
                                objSalaryFormula.coefficientovertimenightsunday;
                            this.calCoefficientOvertimeNightHoliday.EditValue =
                                objSalaryFormula.coefficientovertimenightholiday;
                            this.cboTaxOvertimeWorkdayType.SelectedIndex = (int)objSalaryFormula.taxovertimeworkdaytype;
                            this.txtTaxOvertimeWorkdayValue.Text = objSalaryFormula.taxovertimeworkdayvalue;
                            this.cboTaxOvertimeSundayType.SelectedIndex = (int)objSalaryFormula.taxovertimesundaytype;
                            this.txtTaxOvertimeSundayValue.Text = objSalaryFormula.taxovertimesundayvalue;
                            this.cboTaxOvertimeHolidayType.SelectedIndex = (int)objSalaryFormula.taxovertimeholidaytype;
                            this.txtTaxOvertimeHolidayValue.Text = objSalaryFormula.taxovertimeholidayvalue;
                            this.cboTaxOvertimeNightWorkdayType.SelectedIndex =
                                (int)objSalaryFormula.taxovertimenightworkdaytype;
                            this.txtTaxOvertimeNightWorkdayValue.Text = objSalaryFormula.taxovertimenightworkdayvalue;
                            this.cboTaxOvertimeNightSundayType.SelectedIndex =
                                (int)objSalaryFormula.taxovertimenightsundaytype;
                            this.txtTaxOvertimeNightSundayValue.Text = objSalaryFormula.taxovertimenightsundayvalue;
                            this.cboTaxOvertimeNightHolidayType.SelectedIndex =
                                (int)objSalaryFormula.taxovertimenightholidaytype;
                            this.txtTaxOvertimeNightHolidayValue.Text = objSalaryFormula.taxovertimenightholidayvalue;
                            this.cboOvertimeSaturdayType.SelectedIndex = (int)objSalaryFormula.overtimesaturdaytype;
                            this.cheIsLateEarly.Checked = (bool)objSalaryFormula.islateearly;
                            this.calLateEarlyMinute.EditValue = objSalaryFormula.lateearlyminute;
                            this.cheIsGroupSalaryOvertime.Checked = (bool)objSalaryFormula.isgroupsalaryovertime;
                            this.cboSalaryHourType.SelectedIndex = (int)objSalaryFormula.salaryhourtype;
                            this.cboRoundNumberSalary.Value = (int)objSalaryFormula.roundnumbersalary;
                        }

                        listSelect =
                            listOption.Where(s => s.description == "salaryoption").ToList();

                        objOption = listSelect.SingleOrDefault(s => s.option_id == "IsAllowanceReCreate");
                        if (objOption != null) cheIsAllowanceReCreate.Checked = bool.Parse(objOption.optionvalue);

                        // load dic_pit
                        _exePit.AddGridControlFromList();

                        #endregion Dinh Muc Tinh Luong

                        break;

                    case TabControl.InBaoCao:

                        #region HeaderReport

                        try
                        {
                            listSelect = listOption.Where(s => s.description == "headerreportformatoption").ToList();

                            objOption = listSelect.SingleOrDefault(s => s.option_id == "Style");
                            if (objOption != null)
                            {
                                if (int.Parse(objOption.optionvalue) == 0)
                                {
                                    this.rdStyleDefault.Checked = true;
                                }
                                else
                                {
                                    this.rdStyleImage.Checked = true;
                                }
                            }

                            objOption = listSelect.SingleOrDefault(s => s.option_id == "IsShowLogo");
                            if (objOption != null) cheIsShowLogo.Checked = bool.Parse(objOption.optionvalue);

                            objOption = listSelect.SingleOrDefault(s => s.option_id == "IsUperCompanyName");
                            if (objOption != null) cheIsUperCompanyName.Checked = bool.Parse(objOption.optionvalue);

                            objOption = listSelect.SingleOrDefault(s => s.option_id == "FontCompanyName");
                            if (objOption != null) txtFontCompanyName.Text = objOption.optionvalue;

                            objOption = listSelect.SingleOrDefault(s => s.option_id == "FontCompanyAddress");
                            if (objOption != null) txtFontCompanyAddress.Text = objOption.optionvalue;

                            try
                            {
                                objOption = listSelect.SingleOrDefault(s => s.option_id == "ImageHeader");
                                if (objOption != null)
                                {
                                    byte[] buffer = Convert.FromBase64String(objOption.optionvalue);
                                    if (buffer.Length != 2)
                                    {
                                        MemoryStream stream = new MemoryStream(buffer);
                                        this.ptImageHeader.Image = Image.FromStream(stream);
                                    }
                                    if (buffer.Length == 2)
                                    {
                                        this.ptImageHeader.Image = null;
                                    }
                                }
                            }
                            catch
                            {
                                this.ptImageHeader.Properties.NullText =
                                    "Tập tin hình ảnh có thể bị xóa hoặc không tồn tại.\nNhấp chuột phải, chọn Load để mở tập tin mới!";
                                this.ptImageHeader.Image = null;
                            }
                            objOption = listSelect.SingleOrDefault(s => s.option_id == "ImageWidth");
                            if (objOption != null) calImageWidth.EditValue = float.Parse(objOption.optionvalue);

                            objOption = listSelect.SingleOrDefault(s => s.option_id == "ImageHeight");
                            if (objOption != null) calImageHeight.EditValue = float.Parse(objOption.optionvalue);

                            FontConverter converter = new FontConverter();
                            this.txtFontCompanyName.Font =
                                converter.ConvertFromString(this.txtFontCompanyName.Text) as Font;
                            this.txtFontCompanyAddress.Font =
                                converter.ConvertFromString(this.txtFontCompanyAddress.Text) as Font;
                        }
                        catch
                        {
                            this.rdStyleDefault.Checked = true;
                            this.cheIsShowLogo.Checked = true;
                            this.cheIsUperCompanyName.Checked = true;
                            this.txtFontCompanyName.Text = "Times New Roman, 11,25pt, style=Bold";
                            this.txtFontCompanyAddress.Text = "Times New Roman, 11,25pt, style=Regular";
                            this.ptImageHeader.Image = null;
                            this.calImageWidth.EditValue = 500;
                            this.calImageHeight.EditValue = 100;
                        }

                        #endregion HeaderReport

                        #region footerReportFormatOption

                        listSelect = listOption.Where(s => s.description == "footerreportformatoption").ToList();

                        try
                        {
                            objOption = listSelect.SingleOrDefault(s => s.option_id == "SpaceAbove");
                            if (objOption != null) calSpaceAbove.EditValue = float.Parse(objOption.optionvalue);

                            objOption = listSelect.SingleOrDefault(s => s.option_id == "HeightFooter");
                            if (objOption != null) calHeightFooter.EditValue = float.Parse(objOption.optionvalue);

                            objOption = listSelect.SingleOrDefault(s => s.option_id == "IsShowFullName");
                            if (objOption != null) cheIsShowFullName.Checked = bool.Parse(objOption.optionvalue);

                            objOption = listSelect.SingleOrDefault(s => s.option_id == "FontPosition");
                            if (objOption != null) txtFontPosition.Text = objOption.optionvalue;

                            objOption = listSelect.SingleOrDefault(s => s.option_id == "FontFullName");
                            if (objOption != null) txtFontFullName.Text = objOption.optionvalue;

                            objOption = listSelect.SingleOrDefault(s => s.option_id == "FontRequire");
                            if (objOption != null) txtFontRequire.Text = objOption.optionvalue;
                        }
                        catch
                        {
                            this.calSpaceAbove.EditValue = 40;
                            this.calHeightFooter.EditValue = 70;
                            this.cheIsShowFullName.Checked = true;
                            this.txtFontPosition.Text = "Times New Roman, 11,25pt, style=Bold";
                            this.txtFontFullName.Text = "Times New Roman, 11,25pt, style=Regular";
                            this.txtFontRequire.Text = "Times New Roman, 9,25pt, style=Italic";
                        }
                        try
                        {
                            FontConverter converter = new FontConverter();
                            this.txtFontPosition.Font = converter.ConvertFromString(this.txtFontPosition.Text) as Font;
                            this.txtFontFullName.Font = converter.ConvertFromString(this.txtFontFullName.Text) as Font;
                            this.txtFontRequire.Font = converter.ConvertFromString(this.txtFontRequire.Text) as Font;
                        }
                        catch
                        {
                        }

                        #endregion footerReportFormatOption

                        // load dic_signer
                        _exeSigner.AddGridControlFromList();

                        break;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void SaveOption(TabControl tab)
        {
            try
            {
                List<sys_option> listOption = _listOptions;
                List<sys_option> listSelect;
                sys_option objOption;
                switch (tab)
                {
                    case TabControl.TuyChonChung:

                        #region CauHinhChung

                        listSelect = listOption.Where(s => s.description == "alloption").ToList();

                        objOption = listSelect.SingleOrDefault(s => s.option_id == "MonthDefault");
                        if (objOption != null) objOption.optionvalue = dtMonthDefault.DateTime.ToString("MM-yyyy");
                        _exeOption.Update(objOption);

                        objOption = listSelect.SingleOrDefault(s => s.option_id == "ShowDiagram");
                        if (objOption != null)
                        {
                            objOption.optionvalue = cheShowDiagram.Checked.ToString();
                            _exeOption.Update(objOption);
                        }
                       
                        objOption = listSelect.SingleOrDefault(s => s.option_id == "ShowWorkdesk");
                        if (objOption != null)
                        {
                            objOption.optionvalue = cheShowWorkdesk.Checked.ToString();
                            _exeOption.Update(objOption);
                        }
                       
                        objOption = listSelect.SingleOrDefault(s => s.option_id == "ShowWelcome");
                        if (objOption != null)
                        {
                            objOption.optionvalue = cheShowWelcome.Checked.ToString();
                            _exeOption.Update(objOption);
                        }
                     
                        objOption = listSelect.SingleOrDefault(s => s.option_id == "ShowTimekeeper");
                        if (objOption != null)
                        {
                            objOption.optionvalue = cheShowTimekeeper.Checked.ToString();
                            _exeOption.Update(objOption);
                        }
                     
                        objOption = listSelect.SingleOrDefault(s => s.option_id == "ShowSalary");
                        if (objOption != null)
                        {
                            objOption.optionvalue = cheShowSalary.Checked.ToString();
                            _exeOption.Update(objOption);
                        }
                      
                        objOption = listSelect.SingleOrDefault(s => s.option_id == "Update");
                        if (objOption != null)
                        {
                            objOption.optionvalue = rdUpdate.SelectedIndex.ToString();
                            _exeOption.Update(objOption);
                        }
                      
                        #endregion CauHinhChung

                        break;

                    case TabControl.CauHinhGuiMail:

                        #region CauHinhMail

                        listSelect =
                            listOption.Where(s => s.description == "emailoption").ToList();

                        objOption = listSelect.SingleOrDefault(s => s.option_id == "ShowSendMail");
                        if (objOption != null)
                        {
                            objOption.optionvalue = cheShowSendMail.Checked.ToString();
                            _exeOption.Update(objOption);
                        }

                        objOption = listSelect.SingleOrDefault(s => s.option_id == "Host");
                        if (objOption != null)
                        {
                            objOption.optionvalue = txtHost.Text;
                            _exeOption.Update(objOption);
                        }

                        objOption = listSelect.SingleOrDefault(s => s.option_id == "Port");
                        if (objOption != null)
                        {
                            objOption.optionvalue = calPort.EditValue.ToString();
                            _exeOption.Update(objOption);
                        }

                        objOption = listSelect.SingleOrDefault(s => s.option_id == "EnableSSL");
                        if (objOption != null)
                        {
                            objOption.optionvalue = cheEnableSSL.Checked.ToString();
                            _exeOption.Update(objOption);
                        }

                        #endregion CauHinhMail

                        break;

                    case TabControl.HopDongLaoDong:

                        #region HopDongLaoDong

                        listSelect = listOption.Where(s => s.description == "contractoption").ToList();

                        objOption = listSelect.SingleOrDefault(s => s.option_id == "Signer");
                        if (objOption != null)
                        {
                            objOption.optionvalue = txtSigner.Text;
                            _exeOption.Update(objOption);
                        }

                        objOption = listSelect.SingleOrDefault(s => s.option_id == "SignerNationality");
                        if (objOption != null)
                        {
                            objOption.optionvalue = txtSignerNationality.Text;
                            _exeOption.Update(objOption);
                        }

                        objOption = listSelect.SingleOrDefault(s => s.option_id == "SignerPosition");
                        if (objOption != null)
                        {
                            objOption.optionvalue = txtSignerPosition.Text;
                            _exeOption.Update(objOption);
                        }

                        objOption = listSelect.SingleOrDefault(s => s.option_id == "FilePath0");
                        if (objOption != null)
                        {
                            objOption.optionvalue = txtFilePath0.Text;
                            _exeOption.Update(objOption);
                        }

                        objOption = listSelect.SingleOrDefault(s => s.option_id == "FilePath1");
                        if (objOption != null)
                        {
                            objOption.optionvalue = txtFilePath1.Text;
                            _exeOption.Update(objOption);
                        }

                        objOption = listSelect.SingleOrDefault(s => s.option_id == "FilePath2");
                        if (objOption != null)
                        {
                            objOption.optionvalue = txtFilePath2.Text;
                            _exeOption.Update(objOption);
                        }

                        objOption = listSelect.SingleOrDefault(s => s.option_id == "FilePath3");
                        if (objOption != null)
                        {
                            objOption.optionvalue = txtFilePath3.Text;
                            _exeOption.Update(objOption);
                        }

                        objOption = listSelect.SingleOrDefault(s => s.option_id == "NumberDayWarning");
                        if (objOption != null)
                        {
                            objOption.optionvalue = calNumberDayWarning.EditValue.ToString();
                            _exeOption.Update(objOption);
                        }

                        #endregion HopDongLaoDong

                        break;

                    case TabControl.ChamCong:

                        #region Cham Cong

                        //---------------------------------------------------timekeeperoption------------------------------------------------------------------------------------------------

                        listSelect = listOption.Where(s => s.description == "timekeeperoption").ToList();

                        objOption = listSelect.SingleOrDefault(s => s.option_id == "TimeKeeperTableDefault");
                        if (objOption != null)
                        {
                            objOption.optionvalue = cboTimeKeeperTableDefault.SelectedIndex.ToString();
                            _exeOption.Update(objOption);
                        }
                          

                        objOption = listSelect.SingleOrDefault(s => s.option_id == "ShowAssignmentDialog");
                        if (objOption != null)
                        {
                            objOption.optionvalue = cheShowAssignmentDialog.Checked.ToString();
                            _exeOption.Update(objOption);
                        }

                        objOption = listSelect.SingleOrDefault(s => s.option_id == "ShowPetitionDialog");
                        if (objOption != null)
                        {
                            objOption.optionvalue = cheShowPetitionDialog.Checked.ToString();
                            _exeOption.Update(objOption);
                        }

                        objOption = listSelect.SingleOrDefault(s => s.option_id == "AutoAssignment");
                        if (objOption != null)
                        {
                            objOption.optionvalue = cheAutoAssignment.Checked.ToString();
                            _exeOption.Update(objOption);
                        }

                        objOption = listSelect.SingleOrDefault(s => s.option_id == "AutoPetition");
                        if (objOption != null)
                        {
                            objOption.optionvalue = cheAutoPetition.Checked.ToString();
                             _exeOption.Update(objOption);
                        }

                        objOption = listSelect.SingleOrDefault(s => s.option_id == "MondayColor");
                        if (objOption != null)
                        {
                            objOption.optionvalue = colorMonday.Color.ToArgb().ToString();
                            _exeOption.Update(objOption);
                        }

                        objOption = listSelect.SingleOrDefault(s => s.option_id == "TuesdayColor");
                        if (objOption != null)
                        {
                            objOption.optionvalue = colorTuesday.Color.ToArgb().ToString();
                            _exeOption.Update(objOption);
                        }

                        objOption = listSelect.SingleOrDefault(s => s.option_id == "WednesdayColor");
                        if (objOption != null)
                        {
                            objOption.optionvalue = colorWednesday.Color.ToArgb().ToString();
                            _exeOption.Update(objOption);
                        }

                        objOption = listSelect.SingleOrDefault(s => s.option_id == "ThursdayColor");
                        if (objOption != null)
                        {
                            objOption.optionvalue = colorThursday.Color.ToArgb().ToString();
                            _exeOption.Update(objOption);
                        }

                        objOption = listSelect.SingleOrDefault(s => s.option_id == "FridayColor");
                        if (objOption != null)
                        {
                            objOption.optionvalue = colorFriday.Color.ToArgb().ToString();
                            _exeOption.Update(objOption);
                        }

                        objOption = listSelect.SingleOrDefault(s => s.option_id == "SaturdayColor");
                        if (objOption != null)
                        {
                            objOption.optionvalue = colorSaturday.Color.ToArgb().ToString();
                            _exeOption.Update(objOption);
                        }

                        objOption = listSelect.SingleOrDefault(s => s.option_id == "SundayColor");
                        if (objOption != null)
                        {
                            objOption.optionvalue = colorSunday.Color.ToArgb().ToString();
                            _exeOption.Update(objOption);
                        }

                        objOption = listSelect.SingleOrDefault(s => s.option_id == "HolidayColor");
                        if (objOption != null)
                        {
                            objOption.optionvalue = colorHoliday.Color.ToArgb().ToString();
                            _exeOption.Update(objOption);
                        }

                        //-----------------------------TimekeeperFormula---------------------------------------------------------------------------------------------------------------

                        dic_timekeeper_formula obiTimekeeperFormula =
                            _listTimekeeperFormulas.SingleOrDefault(
                                s => s.formulaid == Guid.Parse("00000000-0000-0000-0000-000000000000"));

                        if (obiTimekeeperFormula != null)
                        {
                            //saturday
                            obiTimekeeperFormula.isovertimesaturday = cheIsOvertimeSaturday.Checked;
                            obiTimekeeperFormula.overtimesaturdaytype = cboOvertimeSaturdayType1.SelectedIndex;
                            obiTimekeeperFormula.begintimesaturday = DateTime.Parse(teBeginTimeSaturday.EditValue.ToString());
                            obiTimekeeperFormula.minimumminutesaturday = int.Parse(calMinimumMinuteSaturday.EditValue.ToString());
                            obiTimekeeperFormula.maximumminutesaturday = int.Parse(calMaximumMinuteSaturday.EditValue.ToString());

                            //sunday
                            obiTimekeeperFormula.isovertimesunday = cheIsOvertimeSunday.Checked;
                            obiTimekeeperFormula.overtimesundaytype = cboOvertimeSundayType.SelectedIndex;
                            obiTimekeeperFormula.begintimesunday = DateTime.Parse(teBeginTimeSunday.EditValue.ToString());
                            obiTimekeeperFormula.minimumminutesunday = int.Parse(calMinimumMinuteSunday.EditValue.ToString());
                            obiTimekeeperFormula.maximumminutesunday = int.Parse(calMaximumMinuteSunday.EditValue.ToString());

                            //Holiday
                            obiTimekeeperFormula.isovertimeholiday = cheIsOvertimeHoliday.Checked;
                            obiTimekeeperFormula.overtimeholidaytype = cboOvertimeHolidayType.SelectedIndex;
                            obiTimekeeperFormula.begintimeholiday = DateTime.Parse(teBeginTimeHoliday.EditValue.ToString());
                            obiTimekeeperFormula.minimumminuteholiday = int.Parse(calMinimumMinuteHoliday.EditValue.ToString());
                            obiTimekeeperFormula.maximumminuteholiday = int.Parse(calMaximumMinuteHoliday.EditValue.ToString());

                            //
                            obiTimekeeperFormula.begintimenight = DateTime.Parse(teBeginTimeNight.EditValue.ToString());
                            obiTimekeeperFormula.endtimenight = DateTime.Parse(teEndTimeNight.EditValue.ToString());
                            obiTimekeeperFormula.numberdaystandard = Double.Parse(calNumberDayStandard.EditValue.ToString());
                            obiTimekeeperFormula.numberhourstandard = Double.Parse(calNumberHourStandard.EditValue.ToString());
                        }

                        #endregion Cham Cong

                        break;

                    case TabControl.MucLuongToiThieu:

                        #region Muc Luong Toi Thieu

                        dic_minimumsalary objMinimumsalary =
                            _listMinimumsalaries.SingleOrDefault(
                                s => s.minimumsalaryid == Guid.Parse("72B634FD-CFDB-409B-BA6F-66D34A6BECC9"));
                        if (objMinimumsalary != null)
                        {
                            objMinimumsalary.money = decimal.Parse(calMoney.EditValue.ToString());
                            objMinimumsalary.date = dtDate.DateTime;
                            objMinimumsalary.decidenumber = txtDecideNumber.Text;
                            objMinimumsalary.filepath = txtFilePath.Text;
                            objMinimumsalary.person = txtPerson.Text;
                        }

                        #endregion Muc Luong Toi Thieu

                        break;

                    case TabControl.DinhMucTinhLuong:

                        #region Dinh Muc Tinh Luong

                        dic_salary_formula objSalaryFormula =
                            _listSalaryFormulas.SingleOrDefault(
                                s => s.formulaid == Guid.Parse("00000000-0000-0000-0000-000000000000"));
                        if (objSalaryFormula != null)
                        {
                            objSalaryFormula.socialinsurance = Double.Parse(calSocialInsurance.EditValue.ToString());
                            objSalaryFormula.healthinsurance = Double.Parse(calHealthInsurance.EditValue.ToString());
                            objSalaryFormula.unemploymentinsurance = Double.Parse(calUnemploymentInsurance.EditValue.ToString());
                            objSalaryFormula.socialinsurance1 = Double.Parse(calSocialInsurance1.EditValue.ToString());
                            objSalaryFormula.healthinsurance1 = Double.Parse(calHealthInsurance1.EditValue.ToString());
                            objSalaryFormula.unemploymentinsurance1 = Double.Parse(calUnemploymentInsurance1.EditValue.ToString());
                            objSalaryFormula.maximuminsurance = Decimal.Parse(calMaximumInsurance.EditValue.ToString());
                            objSalaryFormula.uniontype = int.Parse(cboUnionType.SelectedIndex.ToString());
                            objSalaryFormula.unionvalue = Decimal.Parse(calUnionValue.EditValue.ToString());
                            objSalaryFormula.uniontype1 = int.Parse(cboUnionType1.SelectedIndex.ToString());
                            objSalaryFormula.unionvalue1 = Decimal.Parse(calUnionValue1.EditValue.ToString());
                            objSalaryFormula.maximumunion = Decimal.Parse(calMaximumUnion.EditValue.ToString());
                            objSalaryFormula.incometax = Decimal.Parse(calIncomeTax.EditValue.ToString());
                            objSalaryFormula.personincometax = Decimal.Parse(calPersonIncomeTax.EditValue.ToString());
                            objSalaryFormula.coefficientovertimeworkday = Double.Parse(calCoefficientOvertimeWorkday.EditValue.ToString());
                            objSalaryFormula.coefficientovertimesunday = Double.Parse(calCoefficientOvertimeSunday.EditValue.ToString());
                            objSalaryFormula.coefficientovertimeholiday = Double.Parse(calCoefficientOvertimeHoliday.EditValue.ToString());
                            objSalaryFormula.coefficientovertimeworkday = Double.Parse(calCoefficientOvertimeNightWorkday.EditValue.ToString());
                            objSalaryFormula.coefficientovertimenightsunday = Double.Parse(calCoefficientOvertimeNightSunday.EditValue.ToString());
                            objSalaryFormula.coefficientovertimenightholiday = Double.Parse(calCoefficientOvertimeNightHoliday.EditValue.ToString());
                            objSalaryFormula.taxovertimeworkdaytype = int.Parse(cboTaxOvertimeWorkdayType.SelectedIndex.ToString());
                            objSalaryFormula.taxovertimeworkdayvalue = txtTaxOvertimeWorkdayValue.Text;

                            objSalaryFormula.taxovertimesundaytype = int.Parse(cboTaxOvertimeSundayType.SelectedIndex.ToString());
                            objSalaryFormula.taxovertimesundayvalue = txtTaxOvertimeSundayValue.Text;

                            objSalaryFormula.taxovertimeholidaytype = int.Parse(cboTaxOvertimeHolidayType.SelectedIndex.ToString());
                            objSalaryFormula.taxovertimeholidayvalue = txtTaxOvertimeHolidayValue.Text;

                            objSalaryFormula.taxovertimenightworkdaytype = int.Parse(cboTaxOvertimeNightWorkdayType.SelectedIndex.ToString());
                            objSalaryFormula.taxovertimenightworkdayvalue = txtTaxOvertimeNightWorkdayValue.Text;

                            objSalaryFormula.taxovertimenightsundaytype = int.Parse(cboTaxOvertimeNightSundayType.SelectedIndex.ToString());
                            objSalaryFormula.taxovertimenightsundayvalue = txtTaxOvertimeNightSundayValue.Text;

                            objSalaryFormula.taxovertimenightholidaytype = int.Parse(cboTaxOvertimeNightHolidayType.SelectedIndex.ToString());
                            objSalaryFormula.taxovertimenightholidayvalue = txtTaxOvertimeNightHolidayValue.Text;
                            objSalaryFormula.overtimesaturdaytype = int.Parse(cboOvertimeSaturdayType.SelectedIndex.ToString());
                            objSalaryFormula.islateearly = cheIsLateEarly.Checked;
                            objSalaryFormula.lateearlyminute = Double.Parse(calLateEarlyMinute.EditValue.ToString());
                            objSalaryFormula.isgroupsalaryovertime = cheIsGroupSalaryOvertime.Checked;
                            objSalaryFormula.salaryhourtype = int.Parse(cboSalaryHourType.SelectedIndex.ToString());
                            objSalaryFormula.roundnumbersalary = int.Parse(cboRoundNumberSalary.EditValue.ToString());
                        }

                        listSelect =
                            listOption.Where(s => s.description == "salaryoption").ToList();

                        objOption = listSelect.SingleOrDefault(s => s.option_id == "IsAllowanceReCreate");
                        if (objOption != null)
                        {
                            objOption.optionvalue = cheIsAllowanceReCreate.Checked.ToString();
                            _exeOption.Update(objOption);
                        }

                        // load dic_pit
                        //_exePit.AddGridControlFromList();

                        #endregion Dinh Muc Tinh Luong

                        break;

                    case TabControl.InBaoCao:

                        #region HeaderReport

                        try
                        {
                            listSelect = listOption.Where(s => s.description == "headerreportformatoption").ToList();

                            objOption = listSelect.SingleOrDefault(s => s.option_id == "Style");
                            if (objOption != null)
                            {
                                objOption.optionvalue = rdStyleDefault.Checked ? "0" : "1";
                                _exeOption.Update(objOption);
                            }

                            objOption = listSelect.SingleOrDefault(s => s.option_id == "IsShowLogo");
                            if (objOption != null)
                            {
                                objOption.optionvalue = cheIsShowLogo.Checked.ToString();
                                _exeOption.Update(objOption);
                            }

                            objOption = listSelect.SingleOrDefault(s => s.option_id == "IsUperCompanyName");
                            if (objOption != null)
                            {
                                objOption.optionvalue = cheIsUperCompanyName.Checked.ToString();
                                _exeOption.Update(objOption);
                            }

                            objOption = listSelect.SingleOrDefault(s => s.option_id == "FontCompanyName");
                            if (objOption != null)
                            {
                                objOption.optionvalue = txtFontCompanyName.Text;
                                _exeOption.Update(objOption);
                            }

                            objOption = listSelect.SingleOrDefault(s => s.option_id == "FontCompanyAddress");
                            if (objOption != null)
                            {
                                objOption.optionvalue = txtFontCompanyAddress.Text;
                                _exeOption.Update(objOption);
                            }

                            try
                            {
                                objOption = listSelect.SingleOrDefault(s => s.option_id == "ImageHeader");
                                if (objOption != null)
                                {
                                    byte[] buffer = {Convert.ToByte(ptImageHeader.Image)};
                                    objOption.optionvalue = Convert.ToBase64String(buffer);
                                    _exeOption.Update(objOption);
                                }
                            }
                            catch
                            {
                                // ignored
                            }
                            objOption = listSelect.SingleOrDefault(s => s.option_id == "ImageWidth");
                            if (objOption != null)
                            {
                                objOption.optionvalue = calImageWidth.EditValue.ToString();
                                _exeOption.Update(objOption);
                            }

                            objOption = listSelect.SingleOrDefault(s => s.option_id == "ImageHeight");
                            if (objOption != null)
                            {
                                objOption.optionvalue = calImageHeight.EditValue.ToString();
                                _exeOption.Update(objOption);
                            }
                        }
                        catch
                        {
                            // ignored
                        }

                        #endregion HeaderReport

                        #region footerReportFormatOption

                        listSelect = listOption.Where(s => s.description == "footerreportformatoption").ToList();

                        try
                        {
                            objOption = listSelect.SingleOrDefault(s => s.option_id == "SpaceAbove");
                            if (objOption != null)
                            {
                                objOption.optionvalue = calSpaceAbove.EditValue.ToString();
                                _exeOption.Update(objOption);
                            }

                            objOption = listSelect.SingleOrDefault(s => s.option_id == "HeightFooter");
                            if (objOption != null)
                            {
                                objOption.optionvalue = calHeightFooter.EditValue.ToString();
                                _exeOption.Update(objOption);
                            }

                            objOption = listSelect.SingleOrDefault(s => s.option_id == "IsShowFullName");
                            if (objOption != null)
                            {
                                objOption.optionvalue = cheIsShowFullName.Checked.ToString();
                                _exeOption.Update(objOption);
                            }

                            objOption = listSelect.SingleOrDefault(s => s.option_id == "FontPosition");
                            if (objOption != null)
                            {
                                objOption.optionvalue = txtFontPosition.Text;
                                _exeOption.Update(objOption);
                            }

                            objOption = listSelect.SingleOrDefault(s => s.option_id == "FontFullName");
                            if (objOption != null)
                            {
                                objOption.optionvalue = txtFontFullName.Text;
                                _exeOption.Update(objOption);
                            }

                            objOption = listSelect.SingleOrDefault(s => s.option_id == "FontRequire");
                            if (objOption != null)
                            {
                                objOption.optionvalue = txtFontRequire.Text;
                                _exeOption.Update(objOption);
                            }
                        }
                        catch
                        {
                            // ignored
                        }

                        #endregion footerReportFormatOption

                        // load dic_signer
                        //_exeSigner.AddGridControlFromList();

                        break;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        #region Event Hop Dong

        private void txtFilePath0_ButtonClick(object sender, ButtonPressedEventArgs e)
        {
            if (e.Button.Tag.ToString().Contains("Refresh"))
            {
                txtFilePath0.Text = "";
            }
            else
            {
                OpenFileDialog dialog = new OpenFileDialog
                {
                    Filter = @"Rich Text File(*.rtf)|*.rtf",
                    FilterIndex = 0
                };
                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    txtFilePath0.Text = dialog.FileName;
                }
            }
        }

        private void txtFilePath1_ButtonClick(object sender, ButtonPressedEventArgs e)
        {
            if (e.Button.Tag.ToString().Contains("Refresh"))
            {
                txtFilePath1.Text = "";
            }
            else
            {
                OpenFileDialog dialog = new OpenFileDialog
                {
                    Filter = @"Rich Text File(*.rtf)|*.rtf",
                    FilterIndex = 0
                };
                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    txtFilePath1.Text = dialog.FileName;
                }
            }
        }

        private void txtFilePath2_ButtonClick(object sender, ButtonPressedEventArgs e)
        {
            if (e.Button.Tag.ToString().Contains("Refresh"))
            {
                txtFilePath2.Text = "";
            }
            else
            {
                OpenFileDialog dialog = new OpenFileDialog
                {
                    Filter = @"Rich Text File(*.rtf)|*.rtf",
                    FilterIndex = 0
                };
                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    txtFilePath2.Text = dialog.FileName;
                }
            }
        }

        private void txtFilePath3_ButtonClick(object sender, ButtonPressedEventArgs e)
        {
            if (e.Button.Tag.ToString().Contains("Refresh"))
            {
                txtFilePath3.Text = "";
            }
            else
            {
                OpenFileDialog dialog = new OpenFileDialog
                {
                    Filter = @"Rich Text File(*.rtf)|*.rtf",
                    FilterIndex = 0
                };
                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    txtFilePath3.Text = dialog.FileName;
                }
            }
        }

        private void lbFilePath0_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            try
            {
                Process.Start(Application.StartupPath + lbFilePath0.Text.Remove(0, 2));
            }
            catch (Exception exception)
            {
                XtraMessageBox.Show(exception.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
        }

        private void lbFilePath1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            try
            {
                Process.Start(Application.StartupPath + lbFilePath1.Text.Remove(0, 2));
            }
            catch (Exception exception)
            {
                XtraMessageBox.Show(exception.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
        }

        private void lbFilePath2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            try
            {
                Process.Start(Application.StartupPath + lbFilePath2.Text.Remove(0, 2));
            }
            catch (Exception exception)
            {
                XtraMessageBox.Show(exception.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
        }

        private void lbFilePath3_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            try
            {
                Process.Start(Application.StartupPath + lbFilePath3.Text.Remove(0, 2));
            }
            catch (Exception exception)
            {
                XtraMessageBox.Show(exception.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
        }

        #endregion Event Hop Dong

        #region Event Cham Cong

        // TimekeeperFormula

        private void cboOvertimeHolidayType_SelectedIndexChanged(object sender, EventArgs e)
        {
            int index = this.cboOvertimeHolidayType.SelectedIndex;
            this.pnSubOvertimeHoliday.Enabled = index > 0;
        }

        private void cboOvertimeSaturdayType_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.pnSubOvertimeSaturday.Enabled = this.cboOvertimeSaturdayType1.SelectedIndex > 0;
        }

        private void cboOvertimeSundayType_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.pnSubOvertimeSunday.Enabled = this.cboOvertimeSundayType.SelectedIndex > 0;
        }

        private void cheIsOvertimeHoliday_CheckedChanged(object sender, EventArgs e)
        {
            if (cheIsOvertimeHoliday.Checked)
            {
                cboOvertimeHolidayType.Enabled = true;
                this.pnSubOvertimeHoliday.Enabled = cboOvertimeHolidayType.SelectedIndex > 0;
            }
            else
            {
                cboOvertimeHolidayType.Enabled = false;
                this.pnSubOvertimeHoliday.Enabled = false;
            }
        }

        private void cheIsOvertimeSaturday_CheckedChanged(object sender, EventArgs e)
        {
            if (cheIsOvertimeSaturday.Checked)
            {
                cboOvertimeSaturdayType1.Enabled = true;
                pnSubOvertimeSaturday.Enabled = cboOvertimeSaturdayType1.SelectedIndex > 0;
            }
            else
            {
                cboOvertimeSaturdayType1.Enabled = false;
                pnSubOvertimeSaturday.Enabled = false;
            }
        }

        private void cheIsOvertimeSunday_CheckedChanged(object sender, EventArgs e)
        {
            if (cheIsOvertimeSunday.Checked)
            {
                cboOvertimeSundayType.Enabled = true;
                pnSubOvertimeSunday.Enabled = cboOvertimeSundayType.SelectedIndex > 0;
            }
            else
            {
                cboOvertimeSundayType.Enabled = false;
                pnSubOvertimeSunday.Enabled = false;
            }
        }

        //timekeeperoption

        private void btColorDefault_Click(object sender, EventArgs e)
        {
            this.colorMonday.EditValue = SystemColors.Info;
            this.colorTuesday.EditValue = Color.SeaShell;
            this.colorWednesday.EditValue = SystemColors.Info;
            this.colorThursday.EditValue = Color.SeaShell;
            this.colorFriday.EditValue = SystemColors.Info;
            this.colorSaturday.EditValue = Color.SeaShell;
            this.colorSunday.EditValue = Color.Azure;
            this.colorHoliday.EditValue = Color.FromArgb(0xc0, 0xff, 0xc0);
        }

        #endregion Event Cham Cong

        #region Event dinh muc

        private void bbiDelete_ItemClick(object sender, ItemClickEventArgs e)
        {
            this.gbList.DeleteSelectedRows();
        }

        private void cboUnionType_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.lbUnitCofficientUnion.Text = this.cboUnionType.SelectedIndex == 0 ? @"%" : @"VND";
            if (decimal.Parse(this.calUnionValue.Text) > 100M)
            {
                this.calUnionValue.EditValue = 1;
            }
        }

        private void cboUnionType1_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.lbUnitCofficientUnion1.Text = this.cboUnionType1.SelectedIndex == 0 ? @"%" : @"VND";
            if (decimal.Parse(this.calUnionValue1.Text) > 100M)
            {
                this.calUnionValue1.EditValue = 2;
            }
        }

        private void cboTaxOvertimeType_SelectedIndexChanged(object sender, EventArgs e)
        {
            ComboBoxEdit edit = sender as ComboBoxEdit;
            TextEdit txtTaxOvertimeWorkdayValue = new TextEdit();
            LabelControl lbTaxOvertimeWorkdayType = new LabelControl();

            if (edit == this.cboTaxOvertimeWorkdayType)
            {
                txtTaxOvertimeWorkdayValue = this.txtTaxOvertimeWorkdayValue;
                lbTaxOvertimeWorkdayType = this.lbTaxOvertimeWorkdayType;
            }
            else if (edit == this.cboTaxOvertimeSundayType)
            {
                txtTaxOvertimeWorkdayValue = this.txtTaxOvertimeSundayValue;
                lbTaxOvertimeWorkdayType = this.lbTaxOvertimeSundayType;
            }
            else if (edit == this.cboTaxOvertimeHolidayType)
            {
                txtTaxOvertimeWorkdayValue = this.txtTaxOvertimeHolidayValue;
                lbTaxOvertimeWorkdayType = this.lbTaxOvertimeHolidayType;
            }
            else if (edit == this.cboTaxOvertimeNightWorkdayType)
            {
                txtTaxOvertimeWorkdayValue = this.txtTaxOvertimeNightWorkdayValue;
                lbTaxOvertimeWorkdayType = this.lbTaxOvertimeNightWorkdayType;
            }
            else if (edit == this.cboTaxOvertimeNightSundayType)
            {
                txtTaxOvertimeWorkdayValue = this.txtTaxOvertimeNightSundayValue;
                lbTaxOvertimeWorkdayType = this.lbTaxOvertimeNightSundayType;
            }
            else if (edit == this.cboTaxOvertimeNightHolidayType)
            {
                txtTaxOvertimeWorkdayValue = this.txtTaxOvertimeNightHolidayValue;
                lbTaxOvertimeWorkdayType = this.lbTaxOvertimeNightHolidayType;
            }
            if (edit != null && edit.SelectedIndex == 0)
            {
                lbTaxOvertimeWorkdayType.Text = "%";
                txtTaxOvertimeWorkdayValue.Properties.Mask.MaskType = MaskType.Numeric;
                txtTaxOvertimeWorkdayValue.Properties.Mask.EditMask = "";
                txtTaxOvertimeWorkdayValue.Properties.Mask.UseMaskAsDisplayFormat = true;
            }
            else
            {
                lbTaxOvertimeWorkdayType.Text = "";
                txtTaxOvertimeWorkdayValue.Properties.Mask.MaskType = MaskType.RegEx;
                txtTaxOvertimeWorkdayValue.Properties.Mask.EditMask = @"[0-9]/[1-9]";
                txtTaxOvertimeWorkdayValue.Properties.Mask.UseMaskAsDisplayFormat = true;
                txtTaxOvertimeWorkdayValue.Properties.Mask.IgnoreMaskBlank = false;
                txtTaxOvertimeWorkdayValue.Properties.AllowNullInput = DefaultBoolean.False;
            }
        }

        private void cheIsLateEarly_CheckedChanged(object sender, EventArgs e)
        {
            this.calLateEarlyMinute.Enabled = this.cheIsLateEarly.Checked;
            if (!this.cheIsLateEarly.Enabled)
            {
                this.calLateEarlyMinute.Enabled = false;
            }
        }

        private void gbList_InitNewRow(object sender, InitNewRowEventArgs e)
        {
            this.gbList.SetRowCellValue(e.RowHandle, this.colPITID, Guid.NewGuid());
            this.gbList.SetRowCellValue(e.RowHandle, this.colTaxStep, this.gbList.RowCount);
        }

        private void gbList_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                this.gbList.DeleteSelectedRows();
            }
            if (e.KeyCode == Keys.F5)
            {
                _exePit.AddGridControlFromList();
            }
            if (e.KeyCode == Keys.Escape)
            {
                this.gbList.OptionsView.NewItemRowPosition = NewItemRowPosition.Bottom;
            }
        }

        public void SavePit()
        {
            try
            {
                foreach (dic_pit obj in _exePit.GetList())
                {
                    _exePit.Delete(obj);
                }
                DataTable dataTable = this.gcList.DataSource as DataTable;
                if (dataTable != null)
                    foreach (dic_pit objPitInsert in from DataRow row in dataTable.Rows
                                                     where row != null
                                                     select new dic_pit
                                                         {
                                                             frommoney = decimal.Parse(row["FromMoney"].ToString()),
                                                             pitid = new Guid(row["PITID"].ToString()),
                                                             tomoney = decimal.Parse(row["ToMoney"].ToString()),
                                                             taxprogressive = decimal.Parse(row["TaxProgressive"].ToString()),
                                                             taxrate = double.Parse(row["TaxRate"].ToString()),
                                                             taxstep = int.Parse(row["TaxStep"].ToString())
                                                         }
                        )
                    {
                        _exePit.Insert(objPitInsert);
                    }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        #endregion Event dinh muc

        #region Event bao cao

        #region header
        private void btFontCompanyAddress_Click(object sender, EventArgs e)
        {
            FontConverter converter = new FontConverter();
            this.fontDialog1.Font = this.txtFontCompanyAddress.Font;
            if (this.fontDialog1.ShowDialog() != DialogResult.Cancel)
            {
                this.txtFontCompanyAddress.Font = this.fontDialog1.Font;
                this.txtFontCompanyAddress.Text = converter.ConvertToString(this.fontDialog1.Font);
            }
        }

        private void btFontCompanyName_Click(object sender, EventArgs e)
        {
            FontConverter converter = new FontConverter();
            this.fontDialog1.Font = this.txtFontCompanyName.Font;
            if (this.fontDialog1.ShowDialog() != DialogResult.Cancel)
            {
                this.txtFontCompanyName.Font = this.fontDialog1.Font;
                this.txtFontCompanyName.Text = converter.ConvertToString(this.fontDialog1.Font);
            }
        }

        private void ptImageHeader_ImageChanged(object sender, EventArgs e)
        {
            if (this.ptImageHeader.Image != null)
            {
                this.calImageWidth.EditValue = this.ptImageHeader.Image.Width;
                this.calImageHeight.EditValue = this.ptImageHeader.Image.Height;
                this.gbImage.Text = @"Chọn hình ảnh (kích cỡ gốc: " + this.ptImageHeader.Image.Width.ToString() + " x " + this.ptImageHeader.Image.Height.ToString() + ")";
            }
        }

        private void rdStyleDefault_CheckedChanged(object sender, EventArgs e)
        {
            if (this.rdStyleDefault.Checked)
            {
                this.gbDefault.Enabled = true;
                this.gbImage.Enabled = false;
            }
            else
            {
                this.gbDefault.Enabled = false;
                this.gbImage.Enabled = true;
            }
        }
        #endregion

        #region footer
        private void btFontFullName_Click(object sender, EventArgs e)
        {
            FontConverter converter = new FontConverter();
            this.fontDialog1.Font = this.txtFontFullName.Font;
            if (this.fontDialog1.ShowDialog() != DialogResult.Cancel)
            {
                this.txtFontFullName.Font = this.fontDialog1.Font;
                this.txtFontFullName.Text = converter.ConvertToString(this.fontDialog1.Font);
            }
        }

        private void btFontPosition_Click(object sender, EventArgs e)
        {
            FontConverter converter = new FontConverter();
            this.fontDialog1.Font = this.txtFontPosition.Font;
            if (this.fontDialog1.ShowDialog() != DialogResult.Cancel)
            {
                this.txtFontPosition.Font = this.fontDialog1.Font;
                this.txtFontPosition.Text = converter.ConvertToString(this.fontDialog1.Font);
            }
        }

        private void btFontRequire_Click(object sender, EventArgs e)
        {
            FontConverter converter = new FontConverter();
            this.fontDialog1.Font = this.txtFontRequire.Font;
            if (this.fontDialog1.ShowDialog() != DialogResult.Cancel)
            {
                this.txtFontRequire.Font = this.fontDialog1.Font;
                this.txtFontRequire.Text = converter.ConvertToString(this.fontDialog1.Font);
            }
        }

        private void gbBaoCao_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                this.gbList.DeleteSelectedRows();
            }
        }

     

        #endregion

        #endregion

        #region event From

        private void xfrmAllOption_FormClosing(object sender, FormClosingEventArgs e)
        {
            TotalClose();
        }

        private void TotalClose()
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            SaveOption(_currTabControl);
        }

        private void xtraTabOption_SelectedPageChanged(object sender, TabPageChangedEventArgs e)
        {
            if (e.Page == this.tabAllOption)
            {
                LoadAllOptionToUi(TabControl.TuyChonChung);
            }
            else if (e.Page == this.tabSendMail)
            {
                LoadAllOptionToUi(TabControl.CauHinhGuiMail);
            }
            else if (e.Page == this.tabContract)
            {
                LoadAllOptionToUi(TabControl.HopDongLaoDong);
            }
            else if (e.Page == this.tabTimeKeeper)
            {
                LoadAllOptionToUi(TabControl.ChamCong);
            }
            else if (e.Page == this.tabMinimumSalary)
            {
                LoadAllOptionToUi(TabControl.MucLuongToiThieu);
            }
            else if (e.Page == this.tabSalary)
            {
                LoadAllOptionToUi(TabControl.DinhMucTinhLuong);
            }
            else if (e.Page == this.tabReport)
            {
                LoadAllOptionToUi(TabControl.InBaoCao);
            }
            else if (e.Page == this.tabWarning)
            {
                LoadAllOptionToUi(TabControl.CanhBao);
            }
        }

        #endregion event From
    }
}